package cn.scs.common;

import cn.cfg.ConfigParser;
import cn.cfg.Configs;
import cn.cfg.WorkerParser;
import org.dom4j.DocumentException;

import java.util.List;
import java.util.Map;

public class Config {
    // DFS系统 配置项

    // MetaServer 信息
    public static String META_SERVRE_HOST = "127.0.0.1";
    public static int META_SERVRE_PORT = 9527;
    // DataServer 信息
    public static int DATA_SERVRE_PORT = 9526;
    // 心跳时间机制
    public static int TIMEOUT_OF_HEARTBEATS = 20;
    public static int HEARTBEAT_SECS = 3;
    public static int TIME_OUT = 3;   // 超时时间 秒
    // 实际数据储存
    public static String META_STORE_PATH = "./masterItem/MetaData.img";
    public static String DATA_STORE_PATH = "./storage/";
    public static String WORKER_PATH = "./masterItem/cluster.work";
    // 文件分块
    final public static int blockSize = 33554432;   // 32mb 块大小,Socket 最大支持 -HUADD
    final public static int socketSize = 65536;   // 64kb 块大小,Socket 默认最大支持 -HUADD
    // 用户
    public static String USER = "dfs";
    public static String GROUP = "dfsg";

    // 注册 Master 元数据节点配置
    public static void master_Config() {
        try {
            Configs master_cfg = ConfigParser.parse("src/main/conf/master-config.xml");
            META_SERVRE_HOST = master_cfg.getString("master.host", "127.0.0.1");
            META_SERVRE_PORT = master_cfg.getInt("master.port", 9527);
            META_STORE_PATH = master_cfg.getString("storage.path", "D:/DFS/meta");
            // other settings
        } catch (DocumentException de) {
            de.printStackTrace();
        }
    }
    // 注册 Worker 相关配置
    public static void work_Config() {
        try {
            Configs work_cfg = ConfigParser.parse("src/main/conf/work-config.xml");
            DATA_SERVRE_PORT = work_cfg.getInt("work.port", 9526);
            TIMEOUT_OF_HEARTBEATS = work_cfg.getInt("time.down", 20);
            HEARTBEAT_SECS = work_cfg.getInt("time.beat", 3);
            DATA_STORE_PATH = work_cfg.getString("storage.path", "D:/DFS/meta");
            // other settings
        } catch (DocumentException de) {
            de.printStackTrace();
        }
    }
    // 获取 Workers Host : IP
    public static Map<String, String> worker_get() {
        String hostsPath = null;
        if (System.getProperty("os.name").toLowerCase().contains("linux")) {
            System.out.println(">>> Get Linux System Config -- worker hosts IP");
            hostsPath = "/etc/hosts";
        } else if (System.getProperty("os.name").toLowerCase().contains("windows")) {
            System.out.println(">>> Get Windows System Config -- worker hosts IP");
            hostsPath = "C:/Windows/System32/drivers/etc/HOSTS";
        } else {
            System.out.println("ERROR: This System is unknown & unsupportable for SDFS!");
        }
        hostsPath = "./datanodes.properties";   // 覆盖 使用测试配置
        System.out.println("Hosts : " + hostsPath);
        return new WorkerParser(Config.WORKER_PATH).getWorker(hostsPath);
    }
}
